<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: slotMachine/slotMachine.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: slotMachine/slotMachine.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * Class Machine
 * @class
 * @classdesc 老虎机游戏逻辑部分
 * @author pfan
 * 
 * @example
 *  new Machine(this,{
 *     height: 40,  //单个数字高度
 *     len: 10,     //单个项目数字个数
 *     transY1: 0,  //第一列初始位置
 *     num1: 3,     //第一列结束数字
 *     transY2: 0,  //第二列初始位置
 *     num2: 0,     //第二列结束数字
 *     transY3: 0,  //第三列初始位置
 *     num3: 0,     //第三列结束数字
 *     transY4: 0,  //第四列结束数字
 *     num4: 1,     //第四列结束数字
 *     speed: 24,   //速度
 *     callback: (idx, award) => {
 *      //结束回调， 参数对应宫格索引，对应奖项    
 *    }
 *  })
 */
class Machine {
  /**
   * @constructs Machine构造函数
   * @param  {Object} pageContext page路由指针
   * @param  {Object} opts      组件所需参数
   * @param  {Number} opts.height  单个数字高度
   * @param  {Number} opts.len  单个项目数字个数
   * @param  {Number} opts.transY1  第一列初始位置
   * @param  {Number} opts.num1     第一列结束数字
   * @param  {Number} opts.transY2  第二列初始位置
   * @param  {Number} opts.num2     第二列结束数字
   * @param  {Number} opts.transY3  第三列初始位置
   * @param  {Number} opts.num3     第三列结束数字
   * @param  {Number} opts.transY4  第四列初始位置
   * @param  {Number} opts.num4     第四列结束数字
   * @param  {Number} opts.speed    速度
   * @param  {Function} opts.callback    结束回调
   */  
  constructor (pageContext, opts) {
    this.page = pageContext
    this.height = opts.height
    this.len = opts.len
    this.transY1 = opts.transY1
    this.num1 = opts.num1
    this.transY2 = opts.transY2
    this.num2 = opts.num2
    this.transY3 = opts.transY3
    this.num3 = opts.num3
    this.transY4 = opts.transY4
    this.num4 = opts.num4
    this.speed = opts.speed
    this.isStart = false
    this.endCallBack = opts.callback
    this.page.start = this.start.bind(this)
  }

  start () {
    let { isStart, len, height, transY1, transY2, transY3, transY4, speed, num1, num2, num3, num4, endCallBack } = this
    if(isStart)return
    this.isStart = true    
    let totalHeight = height * len 
    let sRange = Math.floor(Math.random()*2 + 2)
    let halfSpeed = speed / 2
    let endDis1 = num1 == 0 ? 10 * height : num1 * height
    let endDis2 = num2 == 0 ? 10 * height : num2 * height
    let endDis3 = num3 == 0 ? 10 * height : num3 * height
    let endDis4 = num4 == 0 ? 10 * height : num4 * height
    let i1 = 1, i2 = 1, i3 = 1, i4 = 1

    this.timer = setInterval(() => {
      if(i1 &lt;= sRange){
        transY1 -= speed
        if(Math.abs(transY1) > totalHeight){
          transY1 = transY1 + totalHeight
          i1++
        }
      }else if(i1 > sRange &amp;&amp; i1 &lt; sRange + 2){
        transY1 -= halfSpeed
        if(Math.abs(transY1) > totalHeight){
          transY1 = transY1 + totalHeight
          i1++
        }      
      }else{
        if(transY1 == endDis1)return
        let dropSpeed = (endDis1 + transY1) / halfSpeed
        dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; 1 ? 1 : dropSpeed
        transY1 -= dropSpeed
        transY1 =  Math.abs(transY1) > endDis1 ? transY1 = -endDis1: transY1
      }

      this.timer1 = setTimeout( () => {
        if(i2 &lt;= sRange){
          transY2 -= speed
          if(Math.abs(transY2) > totalHeight){
            transY2 = transY2 + totalHeight
            i2++
          }
        }else if(i2 > sRange &amp;&amp; i2 &lt; sRange + 2){
          transY2 -= halfSpeed
          if(Math.abs(transY2) > totalHeight){
            transY2 = transY2 + totalHeight
            i2++
          }      
        }else{
          if(transY2 == endDis2)return
          let dropSpeed = (endDis2 + transY2) / halfSpeed
          dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; 1 ? 1 : dropSpeed
          transY2 -= dropSpeed
          transY2 =  Math.abs(transY2) > endDis2 ? transY2 = -endDis2: transY2
        }
      }, 200)

      this.timer2 = setTimeout( () => {
        if(i3 &lt;= sRange){
          transY3 -= speed
          if(Math.abs(transY3) > totalHeight){
            transY3 = transY3 + totalHeight
            i3++
          }
        }else if(i3 > sRange &amp;&amp; i3 &lt; sRange + 2){
          transY3 -= halfSpeed
          if(Math.abs(transY3) > totalHeight){
            transY3 = transY3 + totalHeight
            i3++
          }      
        }else{
          if(transY3 == endDis3)return
          let dropSpeed = (endDis3 + transY3) / halfSpeed
          dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; 1 ? 1 : dropSpeed
          transY3 -= dropSpeed
          transY3 =  Math.abs(transY3) > endDis3 ? transY3 = -endDis3: transY3
        }
      }, 400)     

      this.timer3 = setTimeout( () => {
        if(i4 &lt;= sRange){
          transY4 -= speed
          if(Math.abs(transY4) > totalHeight){
            transY4 = transY4 + totalHeight
            i4++
          }
        }else if(i4 > sRange &amp;&amp; i4 &lt; sRange + 2){
          transY4 -= halfSpeed
          if(Math.abs(transY4) > totalHeight){
            transY4 = transY4 + totalHeight
            i4++
          }      
        }else{
          let dropSpeed = (endDis4 + transY4) / halfSpeed
          if(num4 &lt; 3){
            dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; .1 ? .1 : dropSpeed
          }else if(num4 &lt; 5 &amp;&amp; num4 >= 3) {
            dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; .3 ? .3 : dropSpeed
          }else{
            dropSpeed = dropSpeed > halfSpeed ? halfSpeed : dropSpeed &lt; .3 ? .3 : dropSpeed
          }
          
          transY4 -= dropSpeed
          transY4 =  Math.abs(transY4) > endDis4 ? transY4 = -endDis4: transY4
          if(Math.abs(transY4) >= endDis4){
            clearInterval(this.timer)
            clearTimeout(this.timer1)
            clearTimeout(this.timer2)
            clearTimeout(this.timer3)
            this.isStart = false
            endCallBack &amp;&amp; endCallBack()
            return 
          }          
        }
      }, 600)   


      this.page.setData({
        machine: {
          transY1: transY1,
          transY2: transY2,
          transY3: transY3,
          transY4: transY4
        }
      })

    }, 1000/60)   
  }

  reset () {
    this.transY1 = 0 
    this.transY2 = 0 
    this.transY3 = 0 
    this.transY4 = 0 

    this.page.setData({
      machine: {
        transY1: 0,
        transY2: 0,
        transY3: 0,
        transY4: 0
      }
    })    
  }

}


export default Machine
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Card.html">Card</a></li><li><a href="FruitMachine.html">FruitMachine</a></li><li><a href="Lock.html">Lock</a></li><li><a href="Machine.html">Machine</a></li><li><a href="Scratch.html">Scratch</a></li><li><a href="Shake.html">Shake</a></li><li><a href="Wheel.html">Wheel</a></li></ul><h3>Global</h3><ul><li><a href="global.html#getDis">getDis</a></li><li><a href="global.html#runAsync">runAsync</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Tue Sep 19 2017 21:36:13 GMT+0800 (HKT)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
